﻿//--------------------------------------------------------------------
// 动态报表文本框控件 v1.00
//
// 开发人： 朱明智
// 创建时间： 2019-08-22
//
// 修改人：
// 修改时间：
//--------------------------------------------------------------------

using System.Drawing;

namespace Dynamic.Controller
{
    /// <summary>
    /// Textbox
    /// </summary>
    public class TextBox : ReportController
    {
        #region props

        /// <summary>
        /// 字体
        /// </summary>
        public Font Font
        {
            get;
            set;
        }

        public Color Color
        {
            get;
            set;
        }

        /// <summary>
        /// 控件宽 (cm)
        /// </summary>
        public float Width
        {
            get { return width; }
            internal set { width = value; }
        }

        /// <summary>
        /// 控件高 (cm)
        /// </summary>
        public float Height
        {
            get { return height; }
            set { height = value; }
        }

        /// <summary>
        /// 控件上边距离父容器距离 (cm)
        /// </summary>
        public float Top
        {
            get { return top; }
            set { top = value; }
        }

        /// <summary>
        /// 控件左边距离父容器距离 (cm)
        /// </summary>
        public float Left
        {
            get { return left; }
            internal set { left = value; }
        }

        /// <summary>
        /// 显示的值
        /// </summary>
        public string Value { get; protected set; }

        /// <summary>
        /// 横向对齐方式
        /// </summary>
        public string TextAlign { get; protected set; }

        /// <summary>
        /// 纵向对齐方式
        /// </summary>
        public string VerticalAlign { get; protected set; }

        /// <summary>
        /// 是否加粗
        /// </summary>
        public string FontWeight { get; protected set; }

        /// <summary>
        /// 控件名称
        /// </summary>
        private static int index = 0;

        #endregion

        /// <summary>
        /// 自定义创建一个标题
        /// </summary>
        /// <param name="text">标题文字</param>
        /// <param name="width">宽（cm）</param>
        /// <param name="height">高（cm）</param>
        public TextBox(string text, float height = 0.8f,
            TextAlign textAlign = Dynamic.TextAlign.Left, VerticalAlign verticalAlign = Dynamic.VerticalAlign.Bottom)
        {
            template = TemplateHelper.TextboxTemp;
            Value = text;
            Height = height;
            if (textAlign == Dynamic.TextAlign.Center) TextAlign = "Center";
            if (textAlign == Dynamic.TextAlign.Left) TextAlign = "Left";
            if (textAlign == Dynamic.TextAlign.Right) TextAlign = "Right";

            if (verticalAlign == Dynamic.VerticalAlign.Bottom) VerticalAlign = "Bottom";
            if (verticalAlign == Dynamic.VerticalAlign.Middle) VerticalAlign = "Middle";
            if (verticalAlign == Dynamic.VerticalAlign.Top) VerticalAlign = "Top";
        }


        public override string ToString()
        {
            string size = "<FontSize>10pt</FontSize>";
            string bold = "";
            string fontFamily = "<FontFamily>宋体</FontFamily>";
            string fontStyle = "";
            string textDecoration = "";

            string color = "";

            if (Font != null)
            {
                size = string.Format("<FontSize>{0}pt</FontSize>", Font.Size);
                bold = Font.Bold ? "<FontWeight>Bold</FontWeight>" : "";
                fontFamily = "<FontFamily>" + Font.FontFamily.Name + "</FontFamily>";
                fontStyle = Font.Italic ? "<FontStyle>Italic</FontStyle>" : "";
                if (Font.Strikeout) textDecoration = "<TextDecoration>LineThrough</TextDecoration>";
                if (Font.Underline) textDecoration = "<TextDecoration>Underline</TextDecoration>";
            }
            if (Color.Name != "0")
            {
                color = string.Format("<Color>{0}</Color>", Color.Name);
            }

            return base.ToString().Replace("@Value", Value)
                .Replace("@FontSize", size)
                .Replace("@FontFamily", fontFamily)
                .Replace("@FontWeight", bold)
                .Replace("@FontStyle", fontStyle)
                .Replace("@Color", color)
                .Replace("@TextDecoration", textDecoration)
                .Replace("@Name", (index++).ToString())
                .Replace("@TextAlign", TextAlign)
                .Replace("@VerticalAlign", VerticalAlign);
        }
    }
}